<?php
require_once('scripts/restrict_access.php');
  if ($identity->accessLevel>30){
	header('Location: AccessDenied.php');
	exit;
}
  require_once('scripts/restrict_access.php');
  require_once('scripts/library.php');	 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--

Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Title      : Complimentary
Version    : 1.0
Released   : 20080409
Description: A two-column, fixed-width and lightweight template ideal for 1024x768 resolutions. Suitable for blogs and small websites.

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Search Results</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="CSS/ComplementaryDefault.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<!-- start header -->
<div id="header">
<div id="logo">
    <h1><a href="#">Find-it</a>
</h1></div>
<div id="menu">
  <ul>
    <li class="active"><a href="DBList.php"> Catalogs</a></li>
    <li><a href="UserList.php">Users </a></li>
    <li><a href="ManageLabels.php">Labels</a></li>
    <li><a href="Index.php?logout">Logout </a></li>
    </ul>
</div>
</div>
<!-- end header -->
<!-- start page -->
<div id="page">
  <!-- start content -->
  <div id="content">
    <div class="post">
      <p><br />
      </p>
<h1 class="title">Search Result</h1>



      <div class="searchfield">
        <form method="get" action="SearchResults.php?">
    <fieldset>
      <input name="searchtext" type="text" id="s" value="" />
      <input name="dbId" type="hidden" value="<?php echo $_GET['dbId'];?>" />
      <input type="submit" id="x" value="Search" />
      </fieldset>
  </form>
</div>
<div class="entry">
<?php
$searchtext = $_GET['searchtext'];
		
		$sql = 'SELECT * from `items` WHERE (`itemName` LIKE ? OR `description` LIKE ? OR `keywords` LIKE ?) AND (`accountId` = ?) ORDER BY `containerId`, `position`';

		$result = $dbRead->fetchAll($sql,array("%".$searchtext."%","%".$searchtext."%","%".$searchtext."%",$_SESSION['accountId']));
			   $size_of_result = sizeof($result);
			   
		
		$i = 0;
		$prevId = ($size_of_result>0 ? $result[0]['containerId'] : 0);
	while($i<$size_of_result)
	{?>
			
			
			
		<table width="100%" border="0" cellpadding="3" cellspacing="3" id="pathTable" name="pathTable">
    <tr>
    <?php 
//Calculate path	
// Link to list of catalogs
	$catalogs = '<a href="DBList.php">Catalogs</a>';
// Link to the currrent catalog
	$sql1 = $dbRead->select('dbId');
	$sql1->from('containers');
	$sql1->where('id = ?', $result[$i]['containerId']);
	$thisdbId = $dbRead->fetchAll($sql1);
		
	$sql = 'SELECT dbName FROM `databases` WHERE `dbID`= ? ';
	$dbName = urlencode($dbRead->fetchOne($sql,$thisdbId[0]['dbId']));
	$path="";
// Calculate the link for all the ansestors
	$id = $result[$i]['containerId'];
	while ($id!=0){
		$sql = 'SELECT parentId FROM `containers` WHERE `Id`= ? ';
		$id = $dbRead->fetchOne($sql,$id); // id now points to parent
		if ($id!=0){
			$sql = 'SELECT name FROM `containers` WHERE `id`= ? ';
			$name = urlencode($dbRead->fetchOne($sql,$id));
			$parentURL = "-->".'<a href="DBMain.php?id='.$id.'&amp;name='.$name.'&amp;dbId='.$_GET['dbId'].'">'.urldecode($name).'</a>';
		} else {
			$parentURL = "-->".'<a href="DBMain.php?dbId='.$_GET['dbId'].'&amp;name='.$dbName.'">'.urldecode($dbName).'</a>';
		}
		$path = $parentURL.$path;
	}

	$sql2 = $dbRead->select('name');
	$sql2->from('containers');
	$sql2->where('id = ?', $result[$i]['containerId']);
	$thiscontainer = $dbRead->fetchAll($sql1);
		
	$thiscontainerName = "-->".'<a href="DBMain.php?id='.$result[$i]['containerId'].'&amp;name='.$thiscontainer[0]['name'].'&amp;dbId='.$thisdbId[0]['dbId'].'">'.$thiscontainer[0]['name'].'</a>';
	$path = $catalogs.$path.$thiscontainerName;
	
//Display the path
	echo $path 
	?>
    </tr>
    </table>
    
<!--Create the table and populate it -->
  <table width="100%" border="1" cellpadding="3" cellspacing="3" id="itemsTable">
    <tr>
      <td width="10%"><h3>Position</h3></td>
      <td width="25%"><h3>Item Name</td>
      <td width="55%"><h3>Description</h3></td>
      <td width="10%"><h3>Checked-out</h3></td>
      </tr>
   <?php  
		   
 		while (($i<$size_of_result) && $result[$i]['containerId'] == $prevId){?>
         
          <tr>
    <td><?php echo $result[$i]['position'];?></td>
    <td><?php echo $result[$i]['itemName']; ?></td>
    <td><?php echo $result[$i]['description']; ?></td>
    <td><input name="checkedOut" type="checkbox" value="1" <?php echo ($result[$i]['checkedOut']==1 ? 'checked="checked"' : '');?> /></td>
  </tr><?php $i++; } ( $i<$size_of_result ? $prevId = $result[$i]['containerId'] : $prevId);
  ?>  
  </table>
			
  <p><br /><br />
    </p>
			
		<?php  }
?>



  <p><br />
    </p>
    <!-- The bottomSpace div is for reserving space at the bottom for the footer -->
        <div class="bottomSpace">
          <p><br /><br />
          </p>
        </div>
</div>
</div>
  </div>
  <!-- end content -->
  <!-- start sidebar --><!-- end sidebar -->
  <div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
  <p id="legal">(c) 2012 Find-It. Design by <a href="http://www.freecsstemplates.org/">Free CSS Templates</a>.<br />
	</p>
</div>
<!-- end footer -->
</body>
</html>
